package com.suning.sawp.in.controller;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.suning.sawp.constants.ErrorCodeConstants;
import com.suning.sawp.dto.common.EmployeeInfo;
import com.suning.sawp.dto.common.ReturnMsg;
import com.suning.sawp.in.filter.RequestUserHolder;
import com.suning.sawp.intf.ZoneChangeService;
import com.suning.sawp.service.util.SCMConfigUtil;

/**
 * 店员信息接口 〈一句话功能简述〉<br>
 * 〈功能详细描述〉
 *
 * @author 15050519
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Controller
@RequestMapping("/adminPage/zoneChange")
public class ZoneChangeController extends DjBaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ZoneChangeController.class);

    private static final String DEFULT_STAFFID = "13075226";

    @Resource
    ZoneChangeService zoneChangeService;

    /**
     * 更新大区信息
     * 
     * @param orgZoneCode
     * @param currZoneCode
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/updateZoneCode.do")
    public MyGsonView updateZoneCode(String orgZoneCode, String currZoneCode) {
        EmployeeInfo employeeInfo = RequestUserHolder.getRequestUser();
        ReturnMsg<String> returnMsg = new ReturnMsg<String>();
        LOGGER.info("updateZoneCode input ,orgZoneCode:{},currZoneCode:{}",orgZoneCode,currZoneCode);
        String staffId = SCMConfigUtil.getConfig("updateZoneCodeStaff",DEFULT_STAFFID);
        if (StringUtils.isBlank(staffId)|| employeeInfo == null || StringUtils.isBlank(employeeInfo.getStaffId())) {
            LOGGER.error("updateZoneCode employeeInfo is null");
            returnMsg.setError(ErrorCodeConstants.ERR_0027);
            return setGsonView(returnMsg);
        }
        
        if (StringUtils.isBlank(orgZoneCode) || StringUtils.isBlank(currZoneCode)) {
            LOGGER.error("updateZoneCode input is null");
            returnMsg.setError(ErrorCodeConstants.ERR_0001);
            return setGsonView(returnMsg);
        }
        ReturnMsg<String> result = null;
        if (staffId.equals(employeeInfo.getStaffId())) {
            result = zoneChangeService.updateZoneCode(orgZoneCode, currZoneCode);
        }

        if (result != null && ReturnMsg.SUCCESS.equals(result.getRetFlag())) {
            returnMsg.setData(result.getData());
        } else {
            LOGGER.error("updateZoneCode  result fail  for staffid  " + employeeInfo.getStaffId());
            returnMsg.setErrorCode(ErrorCodeConstants.ERR_0028);
            returnMsg.setRetFlag(ReturnMsg.FAIL);
            returnMsg.setErrorMessage("HR数据中该大区仍有门店存在，请先联系HR修改门店数据再合并");
        }
        return setGsonView(returnMsg);
    }
}
